package org.xl.main.regex;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class RegexValidatorFrame extends JFrame {
    private JTextField regexField;
    private JTextArea testTextArea;
    private JTextArea resultArea;
    private JCheckBox caseInsensitiveCheckBox;
    private JCheckBox multilineCheckBox;
    private JCheckBox dotallCheckBox;
    
    public RegexValidatorFrame() {
        setTitle("正则表达式验证工具");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800, 600);
        setLocationRelativeTo(null);
        setLayout(null); // 使用绝对布局
        
        initUI();
    }
    
    private void initUI() {
        // 正则表达式标签
        JLabel regexLabel = new JLabel("正则表达式:");
        regexLabel.setBounds(20, 20, 100, 25);
        add(regexLabel);
        
        // 正则表达式输入框
        regexField = new JTextField();
        regexField.setBounds(120, 20, 650, 25);
        add(regexField);
        
        // 选项标签
        JLabel optionsLabel = new JLabel("选项:");
        optionsLabel.setBounds(20, 60, 100, 25);
        add(optionsLabel);
        
        // 选项复选框
        caseInsensitiveCheckBox = new JCheckBox("不区分大小写");
        caseInsensitiveCheckBox.setBounds(120, 60, 120, 25);
        add(caseInsensitiveCheckBox);
        
        multilineCheckBox = new JCheckBox("多行模式");
        multilineCheckBox.setBounds(250, 60, 100, 25);
        add(multilineCheckBox);
        
        dotallCheckBox = new JCheckBox("点号匹配所有字符");
        dotallCheckBox.setBounds(360, 60, 150, 25);
        add(dotallCheckBox);
        
        // 测试文本标签
        JLabel testTextLabel = new JLabel("测试文本:");
        testTextLabel.setBounds(20, 100, 100, 25);
        add(testTextLabel);
        
        // 测试文本区域
        testTextArea = new JTextArea();
        testTextArea.setLineWrap(true);
        JScrollPane textScrollPane = new JScrollPane(testTextArea);
        textScrollPane.setBounds(20, 130, 750, 180);
        add(textScrollPane);
        
        // 结果标签
        JLabel resultLabel = new JLabel("匹配结果:");
        resultLabel.setBounds(20, 330, 100, 25);
        add(resultLabel);
        
        // 结果区域
        resultArea = new JTextArea();
        resultArea.setEditable(false);
        resultArea.setForeground(Color.BLUE);
        JScrollPane resultScrollPane = new JScrollPane(resultArea);
        resultScrollPane.setBounds(20, 360, 750, 150);
        add(resultScrollPane);
        
        // 验证按钮
        JButton validateButton = new JButton("验证");
        validateButton.setBounds(650, 520, 120, 30);
        validateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                validateRegex();
            }
        });
        add(validateButton);
        
        // 添加文档监听器，实现实时验证
        DocumentListener documentListener = new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                validateRegex();
            }
            
            @Override
            public void removeUpdate(DocumentEvent e) {
                validateRegex();
            }
            
            @Override
            public void changedUpdate(DocumentEvent e) {
                validateRegex();
            }
        };
        
        regexField.getDocument().addDocumentListener(documentListener);
        testTextArea.getDocument().addDocumentListener(documentListener);
        
        // 添加选项变更监听器
        ActionListener optionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                validateRegex();
            }
        };
        
        caseInsensitiveCheckBox.addActionListener(optionListener);
        multilineCheckBox.addActionListener(optionListener);
        dotallCheckBox.addActionListener(optionListener);
    }
    
    private void validateRegex() {
        String regex = regexField.getText();
        String testText = testTextArea.getText();
        
        if (regex.isEmpty() || testText.isEmpty()) {
            resultArea.setText("请输入正则表达式和测试文本");
            return;
        }
        
        try {
            // 设置正则表达式选项
            int flags = 0;
            if (caseInsensitiveCheckBox.isSelected()) {
                flags |= Pattern.CASE_INSENSITIVE;
            }
            if (multilineCheckBox.isSelected()) {
                flags |= Pattern.MULTILINE;
            }
            if (dotallCheckBox.isSelected()) {
                flags |= Pattern.DOTALL;
            }
            
            Pattern pattern = Pattern.compile(regex, flags);
            Matcher matcher = pattern.matcher(testText);
            
            StringBuilder result = new StringBuilder();
            boolean found = false;
            
            while (matcher.find()) {
                found = true;
                result.append("找到匹配: '").append(matcher.group()).append("'");
                result.append(" 位置: ").append(matcher.start()).append("-").append(matcher.end());
                if (matcher.groupCount() > 0) {
                    result.append(" 分组: ");
                    for (int i = 1; i <= matcher.groupCount(); i++) {
                        result.append("[").append(i).append("]: ").append(matcher.group(i));
                        if (i < matcher.groupCount()) {
                            result.append(", ");
                        }
                    }
                }
                result.append("\n");
            }
            
            if (!found) {
                result.append("未找到匹配项");
            }
            
            resultArea.setText(result.toString());
            
        } catch (PatternSyntaxException e) {
            resultArea.setText("正则表达式语法错误: " + e.getMessage());
        }
    }
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                
                RegexValidatorFrame validator = new RegexValidatorFrame();
                validator.setVisible(true);
            }
        });
    }
}